package com.sq.subject.domain.handler.subject;

import com.sq.subject.common.enums.IsDeletedFlagEnum;
import com.sq.subject.common.enums.SubjectInfoTypeEnum;
import com.sq.subject.domain.bo.SubjectAnswerBO;
import com.sq.subject.domain.bo.SubjectInfoBO;
import com.sq.subject.infra.basic.entity.SubjectJudge;
import com.sq.subject.infra.basic.entity.SubjectMultiple;
import com.sq.subject.infra.basic.service.SubjectJudgeService;
import com.sq.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 判断策略处理器
 * @author quan
 */
@Component
public class JudgeTypeHandler implements SubjectTypeHandler {
    
    @Resource
    private SubjectJudgeService subjectJudgeService;
    
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBo) {
        List<SubjectAnswerBO> optionList = subjectInfoBo.getOptionList();
        SubjectAnswerBO subjectAnswerBO = optionList.get(0);
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(subjectInfoBo.getSubjectId());
        subjectJudge.setIsCorrect(subjectAnswerBO.getIsCorrect());
        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectJudgeService.insert(subjectJudge);
    }

    @Override
    public SubjectInfoBO query(SubjectInfoBO subjectInfoBO) {
        SubjectJudge subjectJudge =  new SubjectJudge();
        subjectJudge.setSubjectId(subjectInfoBO.getId());
        List<SubjectJudge> subjectJudgeList =  subjectJudgeService.queryByCondition(subjectJudge);
        SubjectJudge judge = subjectJudgeList.get(0);
        Integer isCorrect = judge.getIsCorrect();
        subjectInfoBO.setIsCorrect(isCorrect);
        return subjectInfoBO;
    }
}
